package com.imooc.controller;

import com.imooc.constants.PassWordConstants;
import com.imooc.pojo.Users;
import com.imooc.pojo.bo.AddressBO;
import com.imooc.pojo.bo.UserBO;
import com.imooc.service.AddressService;
import com.imooc.service.UserService;
import com.imooc.utils.CookieUtils;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.MD5Utils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author Administrator
 */
@Api(value = "地址接口", tags = {"用于地址管理的相关接口"})
@RestController
@RequestMapping({"/address"})
public class AddressController {

    /**
     * 1. 地址crud
     * 2. 地址设置默认地址
     */

    @Autowired
    private AddressService addressService;

    @ApiOperation(value = "根据用户id获取用户相关联的地址", notes = "根据用户id获取用户相关联的地址", httpMethod = "POST")
    @PostMapping("/list-by-user-id")
    public IMOOCJSONResult listByUserId(@RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("用户名标识不能为空");
        }
        return IMOOCJSONResult.ok(addressService.listByUserId(userId));
    }

    @ApiOperation(value = "用户信息地址", notes = "用户信息地址", httpMethod = "POST")
    @PostMapping("/insert")
    public IMOOCJSONResult insert(@RequestBody AddressBO addressBO) {
        if (Objects.isNull(addressBO)) {
            return IMOOCJSONResult.errorMsg("传入的对象数据不能为空");
        }
        // 插入数据至数据库
        addressService.insert(addressBO);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "根据用户id和用户地址id删除用户地址", notes = "根据用户id和用户地址id删除用户地址", httpMethod = "POST")
    @PostMapping("/delete-by-user-id-and-address-id")
    public IMOOCJSONResult deleteByUserIdAndAddressId(@RequestParam String userId,
                                                      @RequestParam String addressId) {
        if (StringUtils.isBlank(userId) && StringUtils.isBlank(addressId)) {
            return IMOOCJSONResult.errorMsg("用户名标识或用户地址id不能为空");
        }
        // 插入数据至数据库
        addressService.deleteByUserIdAndAddressId(userId, addressId);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "根据用户id和用户地址id设置默认地址", notes = "根据用户id和用户地址id设置默认地址", httpMethod = "POST")
    @PostMapping("/set-default-by-user-id-and-address-id")
    public IMOOCJSONResult setAddressToBeDefault(@RequestParam String userId,
                                                 @RequestParam String addressId) {
        if (StringUtils.isBlank(userId) && StringUtils.isBlank(addressId)) {
            return IMOOCJSONResult.errorMsg("用户名标识或用户地址id不能为空");
        }
        // 插入数据至数据库
        addressService.setAddressToBeDefault(userId, addressId);
        return IMOOCJSONResult.ok();
    }


}